package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Image;

public class ImageDAO {
    
    /**
     * Load Image in Shop.
     * @return List Image
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public static ArrayList<Image> getList() throws Exception {
        ArrayList<Image> list = new ArrayList<Image>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Image>) session.createCriteria(Image.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Image
     * @param Image
     * @throws Exception
     */
    public static void add(Image image) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(image);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Image
     * @param Image
     * @throws Exception
     */
    public static void update(Image image) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(image);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Image
     * @param Image
     * @throws Exception
     */
    public static void delete(Image image) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(image);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Image by ID
     * @param id
     * @return Image
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Image find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Image image = new Image();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            image = (Image) session.get(Image.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return image;
        }
    }

}
